home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / mail / mailias < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.5 KB  |  101 lines

  1. #!/bin/ksh
  2. # @(#) mailias.ksh 1.2 93/07/16
  3. # mailias: sort mail alias & print with maximum-length lines
  4. # 91/01/13 john h. dubois iii (john@armory.com)
  5. # 91/02/25 Converted to use builtin shell sort.
  6. # 93/07/16 Cleaned up a bit.
  7.  
  8. alias istrue="test 0 -ne"
  9. alias isfalse="test 0 -eq"
  10.  
  11. # Print words passed as args with max line length
  12. # Usage: PrintMaxLines <maxlinelength> <sep> <end> word ...
  13. # <sep> is used to separate words on a line.
  14. # <end> is put at the end of each line except the last.
  15. function PrintMaxLines {
  16.     typeset maxline=$1 sep=$2 end=$3 outline= word
  17.     typeset -i printedline=0 extra=${#sep}+${#end}
  18.  
  19.     shift 3
  20.     for word; do
  21.     if [[ $(( ${#outline}+${#word}+$extra )) -gt $maxline ]]; then
  22.         isfalse printedline && printedline=1 || print -r "$end"
  23.         print -nr "$outline"
  24.         outline=$word
  25.     else
  26.         [ -z "$outline" ] && outline=$word || outline="$outline$sep$word"
  27.     fi
  28.     done
  29.     # print final line if it hasn't been printed yet
  30.     istrue printedline && print -r "$end"
  31.     print -r "$outline"
  32. }
  33.  
  34. set -e
  35.  
  36. name=${0##*/}
  37. Syntax="Syntax: $name [-h] [linelength]"
  38.  
  39. if [ "$1" = -h ]; then
  40.     echo \
  41. "$name: sort names on a mail alias and print it with maximum-length lines.
  42. $Syntax
  43.  
  44. $name processes mail aliases that have this form:
  45.  
  46. alias foo user1 user2 user3 user4 user5\
  47. user6 user7 user8
  48.  
  49. There can be any number of lines, each optionally terminated with a backslash.
  50. The first word of the first line must be \"alias\"; the second word is the
  51. alias name.
  52. The alias is read from the input, and a new alias is printed on the output.
  53. The new alias has the word \"alias\", followed by the alias name, followed
  54. by the sorted user names.
  55. As many names as will fit are printed on each line.
  56. The default line length is 79 characters.
  57. A number given on the command line will override the default.
  58. Every line except the last one is terminated with a backslash."
  59.     exit
  60. fi
  61.  
  62. typeset -i maxline=79
  63. case $# in
  64.     0) 
  65.     ;;
  66.     1) 
  67.     maxline=$1;;
  68.     *) 
  69.     echo "$Syntax"
  70.     exit 1;;
  71. esac
  72.  
  73. set -o noglob
  74.  
  75. # read first line
  76. read -r alias name line
  77. if [ -z "$line" ]; then
  78.     print -u2 "Not a good alias."
  79.     exit 1
  80. fi
  81.  
  82. if [ $alias != alias ]; then
  83.     print -u2 'No "alias" keyword.'
  84.     exit 1
  85. fi
  86.  
  87. # Read the alias into var "list";
  88. # remove trailing backslashes and any whitespace that follows them
  89. # (whitespace is removed by read)
  90. list="${line%\\}"
  91. while read -r line; do
  92.     list="$list ${line%\\}"
  93. done 
  94.  
  95. # sort names on list
  96. set -- $list
  97. set -s
  98.  
  99. PrintMaxLines $maxline " " \\ alias "$name" "$@"
  100. print -r ""
  101.